package com.component.swing.listener;

import java.awt.event.MouseEvent;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.event.MouseInputListener;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.internal.dispatcher.thread.DispatcherThread;
import com.internal.scan.ScanFileSystem;
import com.internal.system.System;

@Component
public class MouseListener implements MouseInputListener{
	
	final static Logger log = Logger.getLogger(MouseListener.class);
	private String path;
	
	@Autowired private DispatcherThread dispatcher;
	@Autowired private System system;
	private Map<String, Object> component;
	
	public DispatcherThread getDispatcher() {
		return dispatcher;
	}

	
	public void setDispatcher(DispatcherThread dispatcher) {
		this.dispatcher = dispatcher;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		Object ob = e.getSource();
		if(ob instanceof JButton)
		{
			JButton b =  (JButton)ob;
			log.info(b.getName());
			if(b.getName().equalsIgnoreCase("start"))
				{
					try {
						log.info(system.getPath());
						dispatcher.execute(new ScanFileSystem(system.getPath()));
					} catch (InterruptedException e1) {
						
						log.info(e1);
					} catch (ExecutionException e1) {
						
						log.info(e1);
					}
					
					
					JTextArea text = (JTextArea)system.get("textArea");
					log.info(dispatcher.getResult());
					text.setText(dispatcher.getResult().toString());
					JFrame frame = (JFrame)system.get("frame");
					
					frame.validate();
					frame.repaint();
				}
			
			if(b.getName().equalsIgnoreCase("stop"))
				dispatcher.stop();
		}
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}


	public Map<String, Object> getComponent() {
		return component;
	}


	public void setComponent(Map<String, Object> component) {
		this.component = component;
	}

}
